package com.wing.device.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.device.dao.DeviceWorkshopDao;
import com.wing.device.model.entity.DeviceWorkshop;
import com.wing.device.model.request.DeviceWorkshopForm;
import com.wing.device.model.response.DeviceWorkshopVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 车间表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DeviceWorkshopServiceImpl extends ServiceImpl<DeviceWorkshopDao, DeviceWorkshop> implements DeviceWorkshopService {

    @Override
    public int add(DeviceWorkshopForm deviceWorkshopForm) {
        DeviceWorkshop workshop = baseMapper.selectOne(new LambdaQueryWrapper<DeviceWorkshop>().eq(DeviceWorkshop::getCode, deviceWorkshopForm.getCode()));
        if(workshop!=null){
            throw new BusinessException("编码重复！");
        }
        //校验
        DeviceWorkshop deviceWorkshop =new DeviceWorkshop();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(DeviceWorkshop.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(deviceWorkshopForm, deviceWorkshop, copyOptions);
        return baseMapper.insert(deviceWorkshop);
    }

    @Override
    public List<DeviceWorkshopVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DeviceWorkshopVO> pageList4app(Page page, Map<String, Object> params) {
        List<DeviceWorkshopVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DeviceWorkshopVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
